#!/bin/bash

#
# clusterAddRemoveNode.sh
# created by youngeryoyo
# of qloudgen to support adding or removing one node (includes gtm_proxy , coordinator, datanode)
# 
gtmProxyPort=6666
coordPort=5432
poolerPort=6667
datanodePort=15432
home=/home/pgxc
database=dilithium

if [ $# != 4 ];then
  echo "Usage: clusterAddRemoveNode.sh configFile controllerIp [add | remove] ip"
  exit 1;
fi
configFile=$1
controllerServer=$2
operaType=$3
ip=$4

# check if operation type is add or remove
if [ "$operaType" != "add" ] && [ "$operaType" != "remove" ];then
  echo "add or remove only!"
  exit 2;
fi
 
# load configFile variables
if [ -f $configFile ]; then
  source $configFile
else
  echo "$configFile does not exist!"
  exit 3;
fi

HOME=$home

checkIp () {
  regex="\b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\b"
  check=`echo $1 | egrep $regex | wc -l`
  if [ $check -eq 0 ];then
    echo "The string $1 is not a correct ip!"
    exit 4;
  fi
}

pingIp () {
  result=`ping -c 1 $1 |grep transmitted |awk '{print $4}'`
  if [ $result = "0" ];then
     echo "$ip can not ping!"
     exit 5;
  else
    echo $1
  fi
}

# check ip
checkIp $controllerServer
checkIp $ip
pingIp $ip

# add one node
if [ "$operaType" = "add" ];then
  
  # check if $ip exists in nodes
  for ((i=0; i<${#coordMasterServers[@]}; i++));do
    if [ "${coordMasterServers[$i]}" != "none" ] && [ "${coordMasterServers[$i]}" != "N/A" ]; then
	if [ "${coordMasterServers[$i]}" = "$ip" ];then
            echo "$ip exists!"
            exit 6;
        fi
    fi
  done

  # get next number of gtm_proxy,coordinator,datanode
  gtmProxyNumber=` expr ${#gtmProxyNames[@]} + 1 `
  coordNumber=` expr ${#coordNames[@]} + 1 `
  datanodeNumber=` expr ${#datanodeNames[@]} + 1 `

  echo $gtmProxyNumber $coordNumber $datanodeNumber

  HOME=$home
  # refresh pg_hba.conf
  echo sh refreshPgHba.sh $configFile add node $ip
  ./refreshPgHba.sh $configFile add node $ip
  
  HOME=$home
  # remove dir 
  echo ssh $ip "rm -rf $gtmProxyDir"
  ssh $ip "rm -rf $gtmProxyDir"

  # firstly add gtm_proxy
  echo pgxc_ctl -c $configFile add gtm_proxy gtm_pxy$gtmProxyNumber $ip $gtmProxyPort $gtmProxyDir
  pgxc_ctl -c $configFile add gtm_proxy gtm_pxy$gtmProxyNumber $ip $gtmProxyPort $gtmProxyDir

  HOME=$home
  # remove dir
  echo  ssh $ip "rm -rf $coordMasterDir"
  ssh $ip "rm -rf $coordMasterDir"
  
  # add coordinator master
  echo pgxc_ctl -c $configFile add coordinator master coord$coordNumber $ip $coordPort $poolerPort $coordMasterDir
  pgxc_ctl -c $configFile add coordinator master coord$coordNumber $ip $coordPort $poolerPort $coordMasterDir

  # add pg_hba on exist servers about $database access control
  for ((i=0; i<${#coordMasterServers[@]}; i++));do
    if [ "${coordMasterServers[$i]}" != "none" ] && [ "${coordMasterServers[$i]}" != "N/A" ]; then
       ssh $pgxcUser@$ip "cat >> $coordMasterDir/pg_hba.conf" <<EOF
host $database all ${coordMasterServers[$i]}/32 md5
EOF
    fi
  done  

  # add pg_hba on gtm server about $database access control
  ssh $pgxcUser@$ip "cat >> $coordMasterDir/pg_hba.conf" <<EOF
host $database all $gtmMasterServer/32 md5
EOF
  if [ "$gtmSlave" = "y" ] && [ "$gtmSlaveServer" != "none" ] && [ "$gtmSlaveServer" != "N/A" ]; then
     ssh $pgxcUser@$ip "cat >> $coordMasterDir/pg_hba.conf" <<EOF
host $database all $gtmSlaveServer/32 md5
EOF
  fi

  # add pg_hba on controller server about $database access control
  if [ "$controllerServer" != "" ];then
    ssh $pgxcUser@$ip "cat >> $coordMasterDir/pg_hba.conf" <<EOF
host $database all $controllerServer/32 md5
EOF
  fi

  ssh $pgxcUser@$ip "cat >> $coordMasterDir/pg_hba.conf" <<EOF
host $database all $ip/32 md5
EOF
 
  ssh $pgxcUser@$ip pg_ctl reload -Z coordinator -D $coordMasterDir 
   
  HOME=$home
  # remove dir
  echo ssh $ip "rm -rf $datanodeMasterDir" 
  ssh $ip "rm -rf $datanodeMasterDir"

  # add datanode master
  echo pgxc_ctl -c $configFile add datanode master datanode$datanodeNumber $ip $datanodePort $datanodeMasterDir
  pgxc_ctl -c $configFile add datanode master datanode$datanodeNumber $ip $datanodePort $datanodeMasterDir

  # add pg_hba on exist servers about $database access control
  for ((i=0; i<${#datanodeMasterServers[@]}; i++));do
    if [ "${datanodeMasterServers[$i]}" != "none" ] && [ "${datanodeMasterServers[$i]}" != "N/A" ]; then
       ssh $pgxcUser@$ip "cat >> $datanodeMasterDir/pg_hba.conf" <<EOF
host $database all ${datanodeMasterServers[$i]}/32 md5
EOF
    fi
  done

  # add pg_hba on gtm server about $database access control
  ssh $pgxcUser@$ip "cat >> $datanodeMasterDir/pg_hba.conf" <<EOF
host $database all $gtmMasterServer/32 md5
EOF
  if [ "$gtmSlave" = "y" ] && [ "$gtmSlaveServer" != "none" ] && [ "$gtmSlaveServer" != "N/A" ]; then
     ssh $pgxcUser@$ip "cat >> $datanodeMasterDir/pg_hba.conf" <<EOF
host $database all $gtmSlaveServer/32 md5
EOF
  fi

  # add pg_hba on controller server about $database access control
  if [ "$controllerServer" != "" ];then
    ssh $pgxcUser@$ip "cat >> $datanodeMasterDir/pg_hba.conf" <<EOF
host $database all $controllerServer/32 md5
EOF
  fi

  ssh $pgxcUser@$ip "cat >> $datanodeMasterDir/pg_hba.conf" <<EOF
host $database all $ip/32 md5
EOF

  ssh $pgxcUser@$ip pg_ctl reload -Z datanode -D $datanodeMasterDir

  HOME=$home
  # data move 
  echo sh clusterDataMove.sh $configFile add datanode$datanodeNumber $database
  ./clusterDataMove.sh $configFile add datanode$datanodeNumber $database

  echo "add node($ip) gtm_proxy(proxy$gtmProxyNumber) coordinator(coord$coordNumber) datanode(datanode$datanodeNumber)!"
fi 

# remove one node
if [ "$operaType" = "remove" ];then

  # get gtmProxyName
  for ((i=0; i<${#gtmProxyServers[@]}; i++));do
    if [ "${gtmProxyServers[$i]}" != "none" ] && [ "${gtmProxyServers[$i]}" != "N/A" ]; then
	if [ "${gtmProxyServers[$i]}" = "$ip" ];then
            gtmProxyName=${gtmProxyNames[$i]}
            break
        fi
    fi
  done

  # get coordName
  for ((i=0; i<${#coordMasterServers[@]}; i++));do
    if [ "${coordMasterServers[$i]}" != "none" ] && [ "${coordMasterServers[$i]}" != "N/A" ]; then
	if [ "${coordMasterServers[$i]}" = "$ip" ];then
            coordName=${coordNames[$i]}
            break
        fi
    fi
  done
  
  # get datanodeName
  for ((i=0; i<${#datanodeMasterServers[@]}; i++));do
    if [ "${datanodeMasterServers[$i]}" != "none" ] && [ "${datanodeMasterServers[$i]}" != "N/A" ]; then
	if [ "${datanodeMasterServers[$i]}" = "$ip" ];then
            datanodeName=${datanodeNames[$i]}
            break
        fi
    fi
  done

  echo remove $datanodeName $coordName $gtmProxyName
  
  HOME=$home
  # data move 
  echo sh clusterDataMove.sh $configFile delete $datanodeName $database
  ./clusterDataMove.sh $configFile delete $datanodeName $database

  HOME=$home
  # firstly remove datanode master
  echo pgxc_ctl -c $configFile remove datanode master $datanodeName clean
  pgxc_ctl -c $configFile remove datanode master $datanodeName clean

  HOME=$home
  # remove coordinator master
  echo pgxc_ctl -c $configFile remove coordinator master $coordName clean
  pgxc_ctl -c $configFile remove coordinator master $coordName clean

  HOME=$home
  # remove gtm_proxy
  echo pgxc_ctl -c $configFile remove gtm_proxy $gtmProxyName clean
  pgxc_ctl -c $configFile remove gtm_proxy $gtmProxyName clean

  HOME=$home
  # refresh pg_hba.conf
  echo sh refreshPgHba.sh $configFile remove node $ip
  ./refreshPgHba.sh $configFile remove node $ip

  echo "remove node($ip) gtm_proxy($gtmProxyName) coordinator($coordName) datanode($datanodeName)!"
fi 






